"""
    打包
"""
import os

APP_NAME = "Audio-player"


def three_dot_version():
    master_version = "0"
    secondary_version = "0"
    small_version = "0"
    revised_version = "8"
    print("版本号 version：{}.{}.{}.{}".format(master_version, secondary_version, small_version, revised_version))
    return "{}.{}.{}.{}".format(master_version, secondary_version, small_version, revised_version)


version = three_dot_version()

PY_INSTALLER_SPEC = f"""
# -*- mode: python -*-

import os
import sys
import logging
sys.path.insert(0, os.getcwd())
os.environ["path"] += ";" + os.getcwd()
from kivy_deps import sdl2, glew
from common.kv_load import make_ui_cache

_, kv_imports = make_ui_cache(os.getcwd())
logging.debug("hiddenimports:{APP_NAME}".format(kv_imports))

block_cipher = None

a = Analysis(['main.py'],
             pathex=['./'],
             binaries=[],
             datas=[("./bin", "bin"), ("./image", "image"), ("./music_folder", "music_folder"),
             ("./ui/cache/", "ui\\cache"),
             ("./conf.ini", "."),
             ],
             hiddenimports= kv_imports + ['six','packaging','packaging.version','packaging.specifiers','configparser'],
             hookspath=[],
             runtime_hooks=[],
             excludes=['cv2'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name="{APP_NAME}",
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='image\\Audio_control_small.ico',
          # version='{version}',
          uac_admin=False,
          disable_windowed_traceback=False,
          argv_emulation=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None,
          )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               name="{APP_NAME}")
"""

_package_name = "./build_package.spec"


def create_package_spec():
    if os.path.exists(_package_name):
        os.remove(_package_name)
    with open(_package_name, 'w', encoding="utf-8") as f:
        f.write(PY_INSTALLER_SPEC)


if __name__ == '__main__':
    import PyInstaller.__main__ as py_installer


    def build_main_process():
        import PyInstaller.__main__ as py_installer
        create_package_spec()
        print("=>Build main process...")
        option = [_package_name, "--noconfirm"]
        py_installer.run(option)


    build_main_process()
